package com.cennavi.controller;

import com.cennavi.function.KpiFunction;
import com.cennavi.function.ListSumFunction;
import com.cennavi.mapper.CommonMapper;
import com.cennavi.mapper.QualityMapper;
import com.cennavi.mapper.WeightMapper;
import com.cennavi.service.CommonService;
import com.googlecode.aviator.AviatorEvaluator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lixiaoshuang on 2018/10/16.
 */
@Controller
@RequestMapping("/function")
public class TestController {
    @Autowired
    CommonService commonService;
    @Resource
    WeightMapper weightMapper;
    @Resource
    QualityMapper qualityMapper;

    @RequestMapping("/testkpi")
    public @ResponseBody Object mytest5(){
//        AviatorEvaluator.addFunction(new ListSumFunction());
//        AviatorEvaluator.addFunction(new KpiFunction());
//        Object obj=commonService.getColumnsToShow("quality");
//        List<String> columns=(List<String>)obj;
//        Map<String,Object> env1=new HashMap<>();
//        String fm="";
//        List<String> columnsWeight=new ArrayList<>();
//        for(String item:columns){
//
//            if(!item.equals("month") && !item.equals("year")){
//                continue;
//            }else {
//                if(fm.length()==0){
//                    fm=item+"*"+item+"_wt";
//                }else {
//                    fm=fm+"+"+item+"*"+item+"_wt";
//                }
//                columnsWeight.add(item+"_wt");
//                List list=qualityMapper.queryData(item);
//                Map<String, Object> env = new HashMap<String, Object>();
//                env.put("list", list);
//                env.put("a", 0);
//                env.put("b", 2);
//                env.put("c",2);
//                env.put("d",4);
//                env.put("wt",0.2);
//                env.put("size",list.size());
//                Object result3 = AviatorEvaluator.execute("kpi(((wt*listSum(list,a,b)))/size,(((1-wt)*listSum(list,c,d)))/size)", env);
//                env1.put(item,result3);
//            }
//        }
//        List<Map> maplist=weightMapper.queryData(columnsWeight);
//        Map map=maplist.get(0);
//        map.putAll(env1);
//        System.out.println("公式为："+fm);
//        Object result4=AviatorEvaluator.execute(fm,env1);
//        System.out.println("结果为："+fm);
//
//        return result4;

        Map<String, Object> env = new HashMap<String, Object>();
        Map<String,Object> env1=new HashMap<>();
        Map<String,Object> env2=new HashMap<>();
        for(int i=0;i<5;i++){
            List list = new ArrayList();
            list.add(10.0+i);
            list.add(20.0+i);
            list.add(10.0+i);
            list.add(10.0+i);
            env.put("list", list);
            env.put("a", 0);
            env.put("b", 2);
            env.put("c",2);
            env.put("d",4);
            env.put("wt",0.2);
            env.put("size",list.size());
            AviatorEvaluator.addFunction(new ListSumFunction());
            AviatorEvaluator.addFunction(new KpiFunction());

            Object result3 = AviatorEvaluator.execute("kpi(((wt*listSum(list,a,b)))/size,(((1-wt)*listSum(list,c,d)))/size)", env);
            env1.put("wt"+i,i/10.00);
            env2.put("wt"+i+"kpi",result3);
        }
        String fm="";
        for (String key : env1.keySet()) {
            if(fm.length()==0){
                fm=key+"*"+key+"kpi";
            }else {
                fm=fm+"+"+key+"*"+key+"kpi";
            }
        }
        env1.putAll(env2);
        System.out.println("公式为："+fm);
        Object result4=AviatorEvaluator.execute(fm,env1);
        System.out.println("结果为："+fm);

        return result4;


    }
    public @ResponseBody Object mytest6(){
        Map<String, Object> env = new HashMap<String, Object>();
        Map<String,Object> env1=new HashMap<>();
        Map<String,Object> env2=new HashMap<>();
        for(int i=0;i<5;i++){
            List list = new ArrayList();
            list.add(10.0+i);
            list.add(20.0+i);
            list.add(10.0+i);
            list.add(10.0+i);
            env.put("list", list);
            env.put("a", 0);
            env.put("b", 2);
            env.put("c",2);
            env.put("d",4);
            env.put("wt",0.2);
            env.put("size",list.size());
            AviatorEvaluator.addFunction(new ListSumFunction());
            AviatorEvaluator.addFunction(new KpiFunction());

            Object result3 = AviatorEvaluator.execute("kpi(((wt*listSum(list,a,b)))/size,(((1-wt)*listSum(list,c,d)))/size)", env);
            env1.put("wt"+i,i/10.00);
            env2.put("wt"+i+"kpi",result3);
        }
        String fm="";
        for (String key : env1.keySet()) {
            if(fm.length()==0){
                fm=key+"*"+key+"kpi";
            }else {
                fm=fm+"+"+key+"*"+key+"kpi";
            }
        }
        env1.putAll(env2);
        System.out.println("公式为："+fm);
        Object result4=AviatorEvaluator.execute(fm,env1);
        System.out.println("结果为："+fm);

        return result4;


    }
}
